package com.buaa.aimall.modelModule.Enum;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.buaa.aimall.modelModule.Entity.ModelEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.function.Consumer;

@Getter
@AllArgsConstructor
public enum ModelSortTypeEnum {
    NAME_EN_ASC("name_en_asc", wrapper -> wrapper.orderByAsc(ModelEntity::getNameEN)),
    NAME_EN_DESC("name_en_desc", wrapper -> wrapper.orderByDesc(ModelEntity::getNameEN)),
    NAME_ZH_ASC("name_zh_asc", wrapper -> wrapper.orderByAsc(ModelEntity::getNameZH)),
    NAME_ZH_DESC("name_zh_desc", wrapper -> wrapper.orderByDesc(ModelEntity::getNameZH)),
    CREATE_TIME_ASC("create_time_asc", wrapper -> wrapper.orderByAsc(ModelEntity::getCreateTime)),
    CREATE_TIME_DESC("create_time_desc", wrapper -> wrapper.orderByDesc(ModelEntity::getCreateTime)),
    UPDATE_TIME_ASC("update_time_asc", wrapper -> wrapper.orderByAsc(ModelEntity::getUpdateTime)),
    UPDATE_TIME_DESC("update_time_desc", wrapper -> wrapper.orderByDesc(ModelEntity::getUpdateTime)),
    MEMORY_ASC("memory_asc", wrapper -> wrapper.orderByAsc(ModelEntity::getMemorySize)),
    MEMORY_DESC("memory_desc", wrapper -> wrapper.orderByDesc(ModelEntity::getMemorySize)),
    RATING_ASC("rating_asc", wrapper -> wrapper.orderByAsc(ModelEntity::getRating)),
    RATING_DESC("rating_desc", wrapper -> wrapper.orderByDesc(ModelEntity::getRating)),
    TYPE_ASC("type_asc", wrapper -> wrapper.orderByAsc(ModelEntity::getType)),
    TYPE_DESC("type_desc", wrapper -> wrapper.orderByDesc(ModelEntity::getType)),
    RANDOM("random", wrapper -> wrapper.last("ORDER BY RAND()")),
    DEFAULT("default", wrapper -> wrapper.orderByDesc(ModelEntity::getCreateTime));

    private final String code;
    private final Consumer<LambdaQueryWrapper<ModelEntity>> sortFunction;

    public static ModelSortTypeEnum fromCode(String code) {
        if (code == null) {
            return DEFAULT;
        }
        for (ModelSortTypeEnum type : values()) {
            if (type.getCode().equals(code)) {
                return type;
            }
        }
        return DEFAULT;
    }
} 